﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using WebChatApi.Common.Interface;

namespace WebChatApi.Common
{
    /// <summary>
    /// Class for web action track.
    /// </summary>
    [DataContract]
    public class WebActionTrack : IIdentifiable
    {
        /// <summary>
        /// The template refer format
        /// </summary>
        public const string TemplateReferFormat = "{0}|{1}";

        #region Properties

        /// <summary>
        /// Gets the key.
        /// The unique id for log.
        /// </summary>
        /// <value>
        /// The key.
        /// </value>
        [DataMember]
        public Guid Key
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the user key.
        /// </summary>
        /// <value>
        /// The user key.
        /// </value>
        [DataMember]
        public Guid UserKey
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the ip address.
        /// </summary>
        /// <value>
        /// The ip address.
        /// </value>
        [DataMember]
        public string IpAddress
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the agent.
        /// </summary>
        /// <value>
        /// The agent.
        /// </value>
        [DataMember]
        public string Agent
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the refer.
        /// If is from email, this field can be template key + template stamp identity
        /// </summary>
        /// <value>
        /// The refer.
        /// </value>
        [DataMember]
        public string Refer
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the target.
        /// e.g: Url, internal interface name, UI page identity or other identity,.
        /// This property value would be matched with <c>Refer</c> property of another log.
        /// </summary>
        /// <value>
        /// The target.
        /// </value>
        [DataMember]
        public string Target
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the created stamp.
        /// </summary>
        /// <value>
        /// The created stamp.
        /// </value>
        [DataMember]
        public DateTime CreatedStamp
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the request data.
        /// e.g.: Id, criteria, POST data, GET data, etc.
        /// </summary>
        /// <value>
        /// The request data.
        /// </value>
        [DataMember]
        public string RequestData
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the service version.
        /// e.g.: Health Service 1.1.2.1602
        /// </summary>
        /// <value>
        /// The service version.
        /// </value>
        [DataMember]
        public string ServiceVersion
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the client version.
        /// e.g.: 1.1.2, 2.3 beta, etc.
        /// This property is for mobile apps only.
        /// </summary>
        /// <value>
        /// The client version.
        /// </value>
        [DataMember]
        public string ClientVersion
        {
            get;
            set;
        }


        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="WebActionTrack"/> class.
        /// </summary>
        public WebActionTrack()
        {
            this.Key = Guid.NewGuid();
            this.CreatedStamp = DateTime.UtcNow;
        }

        /// <summary>
        /// Sets the refer.
        /// </summary>
        /// <param name="templateKey">The template key.</param>
        /// <param name="templateStamp">The template stamp.</param>
        public void SetRefer(Guid templateKey, string templateStamp)
        {
            this.Refer = string.Format(TemplateReferFormat, templateKey.ToString(), templateStamp.GetStringValue());
        }
    }
}
